package com.uchengwang.ui.cm_ui.cycle.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWhitTitleNoBackActivity;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.ucity.listener.RxBusResultSubscriber;
import com.uchengwang.ui.utils.ImageManagerUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import cn.finalteam.rxgalleryfinal.bean.ImageCropBean;
import cn.finalteam.rxgalleryfinal.rxbus.event.ImageRadioResultEvent;

/**
 * Created by U城网 on 2018/6/4.
 * 发布链接
 */

public class ReleaseLinkUrlActivity extends BaseWhitTitleNoBackActivity implements View.OnClickListener{

    private EditText url_title, url_url;
    private ImageView url_cover;
    private String chooseImage, ossImage;

    private long cycleId;
    private String cycleName, titleS, urlS;
    private boolean isAllow = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_release_link_url);
        cycleId = getIntent().getLongExtra(BaseConfig.ID, 0);
        cycleName = getIntent().getStringExtra(BaseConfig.PUB_TITLE);

        getRy().setRightText("完成", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                titleS = url_title.getText().toString();
                urlS = url_url.getText().toString();

                if (TextUtils.isEmpty(titleS)){
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "请输入标题");
                    return;
                }
                if (TextUtils.isEmpty(urlS)){
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "请输入链接");
                    return;
                }

                if (!urlS.startsWith("http") && !urlS.startsWith("https")){
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "请输入正确的链接地址");
                    return;
                }

                if (TextUtils.isEmpty(chooseImage)){
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "请选择图片");
                    return;
                }


                if (!isAllow){
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "正在上传");
                    return;
                }
                isAllow = false;
                LoadingUtils.getUtils().loading(ReleaseLinkUrlActivity.this);
                ImageManagerUtils.getUtils().compressAndPost(ReleaseLinkUrlActivity.this, OSSManager.OSS_CITY, chooseImage, 300, new ImageManagerUtils.ImageCompressListener() {
                    @Override
                    public void onResult(int stutas, String cover, String result) {
                        if (!TextUtils.isEmpty(result)){
                            ossImage = result;
                            post();
                        }else {
                            LoadingUtils.getUtils().hide();
                            isAllow = true;
                            UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "图片上传失败, 请重试");
                        }
                    }
                });
            }
        });

        initView();
    }

    private void initView(){
        url_title = (EditText) findViewById(R.id.url_title);
        url_url = (EditText) findViewById(R.id.url_url);
        url_cover = (ImageView) findViewById(R.id.url_cover);
        url_cover.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.url_cover:
                UCommonUtil.getUtil().openGallerySingle(this, new RxBusResultSubscriber<ImageRadioResultEvent>() {
                    @Override
                    public void onEvent(ImageRadioResultEvent event) {
                        ImageCropBean imageCropBean = event.getResult();
                        if (TextUtils.isEmpty(imageCropBean.getThumbnailBigPath())){
                            chooseImage = imageCropBean.getOriginalPath();
                        }else {
                            chooseImage = imageCropBean.getThumbnailBigPath();
                        }

                        ImageManager.getInstance().loadImage(ReleaseLinkUrlActivity.this, url_cover, chooseImage, ImageManager.ImageType.ROUND);
                    }
                });
                break;
        }
    }

    private void post(){
        MapRequest map = new MapRequest();
        if (cycleId >0){
            map.put(BaseConfig.CYCLE_ID, cycleId);
            if (!TextUtils.isEmpty(cycleName)){
                map.put(BaseConfig.CYCLE_NAME, cycleName);
            }
        }
        map.put(BaseConfig.SHARE_TITLE, titleS);
        map.put(BaseConfig.SHARE_COVER, ossImage);
        map.put(BaseConfig.SHARE_TYPE, BaseConfig.CYCLE_LINK_URL);
        map.put(BaseConfig.SHARE_URL, urlS);
        map.put(BaseConfig.UNI_ID, UChengApplication.myUniId);
        map.put(BaseConfig.UNI_NAME, UChengApplication.mySchoolName);

        CycleHttpUtils.getUtils().releaseCycleInfo(this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                isAllow = true;
                if (stutas ==0){
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "发布成功");
                    BaseConfig.isRelease = true;
                    BaseConfig.isReFresh = false;

                    ReleaseLinkUrlActivity.this.finish();
                }else {
                    isAllow = true;
                    UCommonUtil.getUtil().showToast(ReleaseLinkUrlActivity.this, "请重试");
                }
            }
        });
    }
}
